package com.mega.ycy.mixin;

import com.mega.ycy.Yiciyuan_EVENT;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.*;
import net.minecraft.client.renderer.RenderItem;
import org.spongepowered.asm.mixin.Mixin;
import org.spongepowered.asm.mixin.Overwrite;
import org.spongepowered.asm.mixin.Shadow;

import java.util.ArrayList;
import java.util.List;

@Mixin(GuiScreen.class)
public abstract class ScreenMixin {
    @Shadow public List<GuiButton> buttonList;

    @Shadow public Minecraft mc;

    @Shadow public List<GuiLabel> labelList;

    @Shadow protected RenderItem itemRender;

    @Shadow protected FontRenderer fontRenderer;

    @Shadow public int width;

    @Shadow public int height;

    @Shadow public abstract void initGui();

    @Shadow public abstract void onGuiClosed();

    @Overwrite
    public void drawScreen(int mouseX, int mouseY, float partialTicks)
    {
        if (((GuiScreen) (Object)this) instanceof GuiGameOver && Yiciyuan_EVENT.isSafe(mc.player))
            return;
        for (int i = 0; i < this.buttonList.size(); ++i)
        {
            ((GuiButton)this.buttonList.get(i)).drawButton(this.mc, mouseX, mouseY, partialTicks);
        }

        for (int j = 0; j < this.labelList.size(); ++j)
        {
            ((GuiLabel)this.labelList.get(j)).drawLabel(this.mc, mouseX, mouseY);
        }
    }

    @Overwrite
    public void setWorldAndResolution(Minecraft mc, int width, int height)
    {
        this.mc = mc;
        this.itemRender = mc.getRenderItem();
        this.fontRenderer = mc.fontRenderer;
        this.width = width;
        this.height = height;
        if (((GuiScreen) (Object)this) instanceof GuiGameOver && Yiciyuan_EVENT.isSafe(mc.player)) {
            onGuiClosed();
            buttonList = new ArrayList<GuiButton>() {
                @Override
                public boolean add(GuiButton guiButton) {
                    return false;
                }
            };
            return;
        }
        if (!net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(new net.minecraftforge.client.event.GuiScreenEvent.InitGuiEvent.Pre((GuiScreen) (Object)this, this.buttonList)))
        {
            this.buttonList.clear();
            this.initGui();
        }
        net.minecraftforge.common.MinecraftForge.EVENT_BUS.post(new net.minecraftforge.client.event.GuiScreenEvent.InitGuiEvent.Post((GuiScreen) (Object)this, this.buttonList));
    }
}
